package com.colodoo.code.action;

import com.colodoo.code.common.PageInfo;
import com.colodoo.code.common.QueryRequest;
import com.colodoo.code.entity.TableInf;
import com.colodoo.code.service.TableInfService;
import com.colodoo.framework.common.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 数据表控制器
 *
 * @author zhisan
 */
@RestController
@RequestMapping("/code/tableInf")
@Slf4j
public class TableInfAction {

	@Resource
	TableInfService tableInfService;

	/**
	 * 新增数据
	 *
	 * @param tableInf
	 * @return
	 */
	@PostMapping
	public Result saveTableInf(@RequestBody TableInf tableInf) {
		this.tableInfService.saveTableInf(tableInf);
		return Result.ok();
	}

	/**
	 * 根据ID删除数据
	 *
	 * @param tableInfId
	 * @return
	 */
	@DeleteMapping("/{tableInfId}")
	public Result deleteTableInfById(@PathVariable(name = "tableInfId") Integer tableInfId) {
		this.tableInfService.deleteTableInfById(tableInfId);
		return Result.ok();
	}

	/**
	 * 更新数据
	 *
	 * @param tableInfId
	 * @return
	 */
	@PutMapping("/{tableInfId}")
	public Result updateTableInf(@PathVariable(name = "tableInfId") Integer tableInfId, @RequestBody TableInf tableInf) {
		this.tableInfService.updateTableInf(tableInf);
		return Result.ok();
	}

	/**
	 * 查询数据
	 *
	 * @param tableInfy
	 * @return
	 */
	@GetMapping
	public Result getTableInfs(TableInf tableInfy, QueryRequest request) {
		Page<TableInf> tableInfPage = this.tableInfService.getTableInfs(tableInfy, request);
		return Result.ok(new PageInfo<TableInf>(tableInfPage.getTotalElements(), tableInfPage.getContent()));
	}

	/**
	 * 根据模块新增数据表
	 *
	 * @param moduleId
	 * @param tableInf
	 * @return
	 */
	@PostMapping("/{moduleId}")
	public Result saveTableInfByModuleId(@PathVariable(name = "moduleId") Integer moduleId, @RequestBody TableInf tableInf) {
		this.tableInfService.saveTableInfByModuleId(moduleId, tableInf);
		return Result.ok();
	}


}
